/**
 * Created by Administrator on 2016/10/27.
 */

"use strict";
swan_pc
    .directive('treeView',[function(){

        return {
            restrict: 'E',
            templateUrl: 'scripts/services/directive/treeView/treeView.html',
            scope: {
                treeData: '=',
                canChecked: '=',
                textField: '@',
                valueField:'@',
                // itemClicked: '&',
                // itemCheckedChanged: '&',
                itemTemplateUrl: '@',
                selected:'='
            },
            controller:['$scope', function($scope){
                // console.log($scope.treeData);
                $scope.itemExpended = function(item, $event){
                    item.$$isExpend = ! item.$$isExpend;
                    $event.stopPropagation();
                };

                $scope.getItemIcon = function(item){
                    var isLeaf = $scope.isLeaf(item);

                    if(isLeaf){
                        return 'fa fa-leaf';
                    }

                    return item.$$isExpend ? 'fa fa-minus': 'fa fa-plus';
                };

                $scope.isLeaf = function(item){
                    // console.log(item);
                    return !item.subCompanys || !item.subCompanys.length;
                    // }else{
                    // if(item.subCompanys==null){
                    //     return false;
                    //     return true;
                    // }
                };

                $scope.warpCallback = function(callback, item, $event){
                    ($scope[callback] || angular.noop)({
                        $item:item,
                        $event:$event
                    });
                };



                $scope.selected = [];
                var updateSelected = function (action, id) {
                    if (action == 'add' && $scope.selected.indexOf(id) == -1) $scope.selected.push(id);
                    if (action == 'remove' && $scope.selected.indexOf(id) != -1) $scope.selected.splice($scope.selected.indexOf(id), 1);
                };
                //更新某一列数据的选择
                $scope.updateSelection = function ($event, id) {
                    var checkbox = $event.target;
                    var action = (checkbox.checked ? 'add' : 'remove');
                    updateSelected(action, id);
                };
                //全选操作
                $scope.selectAll = function ($event) {
                    var checkbox = $event.target;
                    var action = (checkbox.checked ? 'add' : 'remove');
                    for (var i = 0; i < $scope.treeData.length; i++) {
                        var toDo = $scope.treeData[i];
                        updateSelected(action, toDo.id);
                    }
                };
                $scope.isSelected = function (id) {
                    return $scope.selected.indexOf(id) >= 0;
                };
                $scope.isSelectedAll = function () {
                    return $scope.selected.length === $scope.treeData.length;
                };
            }]
        };
    }]);